
import { getMuLinUser, disabledBtn, enabledBtn, dealResult } from "@/utils/common.js";
import { reqUserUpdatePass } from "@/utils/service.js";
import Bus from '@/utils/bus.js';

export default {
	name: 'cHeader',
	data() {
		return {
			username: "",
			collapse: false,
			fullscreen: false,
			keyWord: "",
			dialogUpdatePwdVisible: false, //修改密码模态框
			updatePwdForm: {
				oldPassword: "", //旧密码
				newPassword: "", //新密码
				checkPassword: "", //确认密码
			},
			updatePwdRules: {
				oldPassword: [{
					required: true,
					message: '请输入旧密码!',
					trigger: "blur"
				}],
				newPassword: [{
					required: true,
					message: '请输入新密码!',
					trigger: "blur"
				}],
				checkPassword: [{
					required: true,
					message: '请输入确认密码!',
					trigger: "blur"
				}],
			},
		}
	},
	mounted() {
		if(Object.keys(getMuLinUser()).length > 0){
			let username = getMuLinUser().realName;
			this.username = username;
		}
	},
	created() {
	},
	components: {},
	methods: {
		//折叠状态改变
		handleCollapseChage() {
			this.collapse = !this.collapse;
			Bus.$emit('collapse', this.collapse);
		},
		//点击用户菜单
		handleUserMenu(val) {
			switch(val){
				case "updatePwd": //修改密码
					this.toggleDialogUpdatePwdVisible();
				break;
				case "loginout": //退出登录
					localStorage.clear();
                	this.$router.push('/login');
				break;
			}
		},
		//显示或隐藏修改密码模态框
		toggleDialogUpdatePwdVisible(){
			let dialogUpdatePwdVisible = this.dialogUpdatePwdVisible;
			this.dialogUpdatePwdVisible = !dialogUpdatePwdVisible;
		},
		//点击修改密码模态框里的确定按钮
		handleUpdatePwdSubmit(formName) {
			let that = this;
			that.$refs[formName].validate((valid) => {
				if(valid) {
					let updatePwdForm = that.updatePwdForm;
					let param = {
						...updatePwdForm
					};
					if(updatePwdForm.newPassword != updatePwdForm.checkPassword){
						this.$message.error('新密码和确认密码不一致!');
						return;
					}
					disabledBtn(".updatePwdBtn");
					reqUserUpdatePass(param, data => {
						if(data.result == "1"){
							that.$message({
								message: '成功!',
								type: 'success'
							});
							that.toggleDialogUpdatePwdVisible();
							that.$refs[formName].resetFields();
						}
						dealResult(data);
						enabledBtn(".updatePwdBtn");
					});
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		//点击全屏按钮
		handleFullScreen() {
			let element = document.documentElement;
			if(this.fullscreen) {
				if(document.exitFullscreen) {
					document.exitFullscreen();
				} else if(document.webkitCancelFullScreen) {
					document.webkitCancelFullScreen();
				} else if(document.mozCancelFullScreen) {
					document.mozCancelFullScreen();
				} else if(document.msExitFullscreen) {
					document.msExitFullscreen();
				}
			} else {
				if(element.requestFullscreen) {
					element.requestFullscreen();
				} else if(element.webkitRequestFullScreen) {
					element.webkitRequestFullScreen();
				} else if(element.mozRequestFullScreen) {
					element.mozRequestFullScreen();
				} else if(element.msRequestFullscreen) {
					// IE11
					element.msRequestFullscreen();
				}
			}
			this.fullscreen = !this.fullscreen;
		},
		getUrlHash(){
			return window.location.hash.split('?')[0].split('/')[1];
		},
		//点击搜索按钮
		handleSearch() {
			let keyWord = this.keyWord;
			if(!keyWord) {
				this.$message("请输入页面代码");
				return;
			}
			let routePath = "";
			this.menuList
				.reduce((list, {
					subs = []
				}) => {
					return list.concat(subs);
				}, [])
				.forEach(item => {
					if(item.code === this.keyWord.toUpperCase()) routePath = item.index;
				});
			if(routePath) {
				if(routePath === this.getUrlHash()) return;
				this.activeRouter  =  routePath.replace('/',  '');
				this.$router.push(routePath);
			} else {
				this.$message("请输入正确的页面代码");
			}
		},
	}
}